package dao

import (
	"context"
	"sale-admin/internal/dal"
	"sale-admin/internal/models"
	"strings"
	"time"

	"192.168.1.75/go-pkg/helper"
)

type RobotEmptyStock struct{}

func (RobotEmptyStock) Create(ctx context.Context, query *dal.Query, Type int, merchantID, robotName, date string,
	goodsID int64, goodsName1 string, goodsInventory, goodsMaxInventory int64, locationID string) (err error) {
	var d models.MaRobotEmptyStock
	d.ID, _ = helper.UUID{}.String()
	d.Type = Type // 1单日空货,2连续空货
	d.MerchantID = merchantID
	d.RobotName = robotName
	d.Date = date
	d.GoodsID = goodsID
	d.GoodsName1 = goodsName1
	d.GoodsInventory = goodsInventory
	d.GoodsMaxInventory = goodsMaxInventory
	d.LocationID = locationID
	d.CreatedAt = time.Now().Unix()

	query.MaRobotEmptyStock.WithContext(ctx).Create(&d)
	return
}

func (RobotEmptyStock) List(ctx context.Context, query *dal.Query, Type int, merchantID, startTime, endTime, robotName string) (
	list []*models.MaRobotEmptyStock, count int64, err error,
) {
	m := query.MaRobotEmptyStock
	tx := m.WithContext(ctx)

	if robotName != "" {
		if strings.Contains(robotName, ",") {
			tx = tx.Where(m.RobotName.In(strings.Split(robotName, ",")...))
		} else {
			tx = tx.Where(m.RobotName.Eq(robotName))
		}
	}
	if startTime != "" && endTime != "" {
		tx = tx.Where(m.Date.Between(startTime, endTime))
	}
	if Type == 1 || Type == 2 {
		tx = tx.Where(m.Type.Eq(Type))
	} else {
		return
	}
	list, count, err = tx.FindByPage(-1, -1)
	if list == nil {
		list = []*models.MaRobotEmptyStock{}
	}
	return
}
